package com.hjj.AppIO;

import java.io.*;

public class MyApp7 {
    public static void main(String[] args) {
        // 缓存容量
        int bufferSize = 1024 * 1024 * 10;

        // 源地址
        String pathSrc = "F:\\动力节点\\第二阶段\\010-spring转换器（资料114页）\\35.Spring MVC学习指南（第2版）.pdf";
        // 目标地址
        String pathDes = "F:\\IO\\test\\Spring MVC学习指南（第2版）.pdf";

        // 目的地判断
        File fileDes = new File(pathDes);
        if (fileDes.exists()) {
            boolean delete = fileDes.delete();
            if (!delete) {
                System.out.println("拷贝失败，文件无法删除");
                return;
            }
        }

        // 创建文件
        boolean newFile = false;
        try {
            newFile = fileDes.createNewFile();
            if (!newFile) {
                System.out.println("文件创建失败...........");
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建文件失败........IO异常");
        }


        // 开始拷贝文件
        try {
            // 开始读取源文件内容
            byte[] bytes = new byte[bufferSize];
            FileInputStream fileInputStream = new FileInputStream(pathSrc);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, bufferSize);

            // 开始准备连接目的地文件
            FileOutputStream fileOutputStream = new FileOutputStream(pathDes);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, bufferSize);

            // 计数器，记录拷贝了多少字节
            int js = 0;

            int read = 0;
            while ((read = bufferedInputStream.read(bytes, 0, bytes.length)) != -1) {
                js += read;
                System.out.println("正在拷贝........." + js);
                bufferedOutputStream.write(bytes, 0, read);
            }

            // 关闭流
            bufferedInputStream.close();
            bufferedOutputStream.close();
            fileInputStream.close();
            fileOutputStream.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件源读取失败...............文件不存在错误");
            return;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件源读取失败...............IO错误");
        }

    }
}
